package cn.git.demo.service.impl;


import cn.git.demo.dao.BrandDao;
import cn.git.demo.pojo.Brand;
import cn.git.demo.pojo.BrandQuery;
import cn.git.demo.service.BrandService;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
@Transactional
public class BrandServiceImpl implements BrandService {


    @Autowired
    private BrandDao brandDao;

    public Pagination selectPaginationByQuery(String name, Integer isDisplay, Integer pageNo) {

        BrandQuery brandQuery = new BrandQuery();

        brandQuery.setPageNo(Pagination.cpn(pageNo));

        brandQuery.setpageSize(3);
//        条件
        StringBuffer str = new StringBuffer();

        if (name != null){
            brandQuery.setName(name);
            str.append("name=").append(name);
        }
        if (isDisplay != null){
            brandQuery.setIsDisplay(isDisplay);
            str.append("&isDisplay=").append(isDisplay);
        }else {
            brandQuery.setIsDisplay(1);
            str.append("&isDisplay=").append(1);
        }

        Pagination pagination = new Pagination(
                brandQuery.getPageNo(),
                brandQuery.getpageSize(),
                brandDao.selectCount(brandQuery)
        );


        pagination.setList(brandDao.selectBrandListByQuery(brandQuery));

        //分页展示

        String url = "/brand/list.do";

        pagination.pageView(url,str.toString());

        return pagination;
    }

    public Brand selectBrandById(Long id) {

        return brandDao.selectBrandById(id);


    }

    public void updateBrand(Brand brand) {

        brandDao.updateBrand(brand);

    }

}
